﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;

namespace Microsoft.CTS.HSG
{
    public sealed class SqlScripterDiagnostics
    {
        private const string LogFormat = "[{0}]{1}";

        public static readonly TraceSwitch ScriptEngineTraceSwitch = new TraceSwitch(
            "SqlScripterEngineTraceSwitch",
            "Trace switch for the SqlScripterEngine assembly."
            );

        public static TraceSwitch globalTraceSwitch;

        public SqlScripterDiagnostics() : this(null)
        {
        }


        public SqlScripterDiagnostics(
            TraceSwitch globalTraceSwitch
            )
        {
            if (globalTraceSwitch == null)
            {
                SqlScripterDiagnostics.globalTraceSwitch = new TraceSwitch(
                    "Internal",
                    "Internal"
                    );

                SqlScripterDiagnostics.globalTraceSwitch.Level = TraceLevel.Off;
            }
            else
            {
                SqlScripterDiagnostics.globalTraceSwitch = globalTraceSwitch;
            }

            SqlScripterDiagnostics.ScriptEngineTraceSwitch.Level = TraceLevel.Verbose;
        }

        public static void Log(
            TraceLevel level,
            string message,
            params object[] args
            )
        {
            if (args == null)
            {
                SqlScripterDiagnostics.Log(
                    level,
                    message
                    );
            }
            else
            {
                string formattedMessage = string.Format(
                    CultureInfo.InvariantCulture,
                    message,
                    args
                    );

                string loggedMessage = string.Format(
                    CultureInfo.InvariantCulture,
                    SqlScripterDiagnostics.LogFormat,
                    DateTime.Now.ToString(),
                    formattedMessage
                    );

                if (SqlScripterDiagnostics.globalTraceSwitch == null)
                {
                    Trace.WriteLineIf(
                    SqlScripterDiagnostics.ScriptEngineTraceSwitch.Level >= level,
                    loggedMessage,
                    "SqlScripterEngineTraceSwitch"
                    );
                }
                else
                {
                    Trace.WriteLineIf(
                    SqlScripterDiagnostics.globalTraceSwitch.Level >= level,
                    loggedMessage,
                    "SqlScripterEngineTraceSwitch"
                    );
                }
            }
        }

        public static void Log(
            TraceLevel level,
            string message
            )
        {
            string formattedMessage = string.Format(
                CultureInfo.InvariantCulture,
                SqlScripterDiagnostics.LogFormat,
                DateTime.Now.ToString(),
                message
                );

            if (SqlScripterDiagnostics.globalTraceSwitch == null)
            {
                Trace.WriteLineIf(
                SqlScripterDiagnostics.ScriptEngineTraceSwitch.Level >= level,
                formattedMessage,
                "SqlScripterEngineTraceSwitch"
                );
            }
            else
            {
                Trace.WriteLineIf(
                SqlScripterDiagnostics.globalTraceSwitch.Level >= level,
                formattedMessage,
                "SqlScripterEngineTraceSwitch"
                );
            }
        }
    }
}
